﻿using UnityEngine;
using System.Collections;

public enum WallType
    {
        NoneWall = 1,
        AllWall = 2,
        RightWall = 3,
        RightBotWall = 4,
        BotWall = 5,
        LeftBotWall = 6,
        LeftWall = 7,
        TopLeftWall = 8,
        TopWall = 9,
        TopRightWall = 10,
        TopBotWall = 11,
        TopLeftBotWall = 12,
        TopRightBotWall = 13,
        LeftRightWall = 14,
        LeftBotRightWall = 15,
        LeftTopRightWall = 16
    }

[System.Serializable]
public class Wall{
	public WallType type;
	public GameObject wallObj;
}

public class LevelGenerator : MonoBehaviour {
	public TextAsset levelFile;
	public Wall[] walls;
	public GameObject checkPointObj, itemObj, mainObj, obstacleObj;
	//public string filePath;
	private char[] delimiters = new char[]{' '};
	private int _checkPoint, _item, _main, _obstacle;
	void Awake () {
	//	filePath =@"C:\Users\Administrator\Desktop\Cubie\Cubie\LevelEditor\Texture\testWall4.txt";
		_checkPoint = 21;
		_item = 22;
		_main = 20;
		_obstacle = 23;
		GenerateLevel();
	}
	
	
	void Update () {
	
	}
	
	public void GenerateLevel(){
		string txt = levelFile.text;
		string[] lines = txt.Split('\n');//System.IO.File.ReadAllLines(filePath);
		for(int i = 0; i < lines.Length; i++){
			string[] elems = lines[i].Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries);
			for(int j = 0; j < elems.Length; j++){
				int elemInt = System.Convert.ToInt32(elems[j]);
				if(elemInt >= (int)WallType.NoneWall && elemInt <= (int)WallType.LeftTopRightWall){
					Instantiate(walls[elemInt - 1].wallObj, new Vector3(j, -i, 0), Quaternion.Euler(0, 180, 0));
				}else if(elemInt == _checkPoint){
					Instantiate(checkPointObj, new Vector3(j + checkPointObj.transform.localScale.x / 2, -i + checkPointObj.transform.localScale.x / 2, 0.5f), Quaternion.identity);
				}else if(elemInt == _item){
					Instantiate(itemObj, new Vector3(j + itemObj.transform.localScale.x / 2, -i + itemObj.transform.localScale.x / 2, 0.5f), Quaternion.identity);
//					Debug.Log("one item");
				}else if(elemInt == _main){
					GameObject tempObj = Instantiate(mainObj, new Vector3(j + itemObj.transform.localScale.x / 2, -i + itemObj.transform.localScale.x / 2, 0.5f), Quaternion.identity) as GameObject;
					tempObj.name = "MainCharacter";
				}else if(elemInt == _obstacle){
					Instantiate(obstacleObj, new Vector3(j + checkPointObj.transform.localScale.x / 2, -i + itemObj.transform.localScale.x / 2, 0.5f), Quaternion.identity);
				}
			}
		}
	}
	
}
